package com.ize.controller.admin;


import com.ize.result.Result;
import com.ize.util.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Api(tags = "文件上传")
public class FileUploadContriller {

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> uploadFile(MultipartFile file) throws Exception {
        //上传的文件内容保存到磁盘上
        //1.获取上传的文件名
        String OriginalFilename = file.getOriginalFilename();
        // 2. 为了防止重名文件覆盖，保证文件名的唯一性，使用UUID和时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        String filename = UUID.randomUUID().toString() + "_" + timestamp+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")) ;

        //3.开始写入
        //file.transferTo(new File("E:\\工具\\IDEA\\CODE\\BigNew\\src\\main\\resources\\touxiang_Data\\"+filename));
        String Url = AliyunOssUtil.picUrl(filename,file.getInputStream());
        return Result.success(Url);
    }
}
